package top.wshape1.shiziapi.search.listener;

import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import top.wshape1.shiziapi.common.domain.ProjectSearch;
import top.wshape1.shiziapi.search.config.RabbitMqConfig;
import top.wshape1.shiziapi.search.service.ProjectSearchService;

import java.io.IOException;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2024-01-21
 */

@Component
@Slf4j
@RabbitListener(queues = RabbitMqConfig.SHIZIAPI_PROJECT_ELASTIC_QUEUE)
public class ProjectSearchListener {

    @Resource
    private ProjectSearchService projectSearchService;

    @RabbitHandler
    public void handleProjectSearch(Channel channel, Message message, ProjectSearch projectSearch) throws IOException {

        log.info("接收到项目搜索消息：{}", projectSearch);
        int operate = projectSearch.getOperate();
        projectSearch.setOperate(null);

        if (operate == 1 || operate == 2)
            projectSearchService.save(projectSearch);
        else if (operate == 3)
            projectSearchService.remove(projectSearch.getId());
        else if (operate == 4)
            projectSearchService.plusOrReduceLike(projectSearch.getId(), "plus", projectSearch.getUpdateTime());
        else if (operate == 5)
            projectSearchService.plusOrReduceLike(projectSearch.getId(), "reduce", projectSearch.getUpdateTime());
        else if (operate == 101)
            projectSearchService.plusRead(projectSearch.getId(), projectSearch.getUpdateTime());

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("项目搜索消息处理完成");

    }

}
